package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.enums.MessageTypeEnum;
import com.useeinfo.oa.modules.base.entity.PlanMessageSend;
import com.useeinfo.oa.modules.security.entity.UserDevice;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.*;

/**
 * @Auther: xudong.sun
 * @Date: 2019/1/18 09:16
 * @Description:
 */
@Repository("planMessageSendDao")
public class PlanMessageSendDao implements CrudDao<PlanMessageSend> {

    private final static Logger logger = LoggerFactory.getLogger(PlanMessageSendDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(s.enterprise.id = ?{paramIndex}) ", StringConverters.null2Long(enterpriseId));
        }

        String messageTypeEnum = queryHash.get("messageTypeEnum");
        if (StringUtils.isNotBlank(messageTypeEnum)) {
            String[] statusArray = messageTypeEnum.split(",");
            for (int i = 0; i < statusArray.length; i++) {
                if (i == 0) {
                    String k = statusArray.length == 1 ? "messageTypeEnum = ?{paramIndex} " : "(messageTypeEnum = ?{paramIndex} ";
                    conditionHash.put(k, MessageTypeEnum.valueOf(statusArray[i].trim()));
                } else if (i == statusArray.length - 1) {
                    conditionHash.put("or messageTypeEnum = ?{paramIndex}) ", MessageTypeEnum.valueOf(statusArray[i].trim()));
                } else {
                    String k = String.format("or messageTypeEnum = ?{paramIndex} %" + i + "s", "");
                    conditionHash.put(k, MessageTypeEnum.valueOf(statusArray[i].trim()));
                }
            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from MaintainOrder s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<PlanMessageSend> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from PlanMessageSend s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<PlanMessageSend> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, PlanMessageSend.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public PlanMessageSend findModel(Long id) {
        return entityManager.find(PlanMessageSend.class, id);
    }

    @Override
    public Integer add(PlanMessageSend model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("PlanMessageSendDaoImpl添加planMessageSend成功！");
        return 1;
    }

    @Override
    public Integer update(PlanMessageSend model) {
        return null;
    }

    @Override
    public Integer delete(Long id) {
        PlanMessageSend planMessageSend = entityManager.find(PlanMessageSend.class, id);
        entityManager.remove(planMessageSend);
        return 1;
    }

    @Override
    public Integer deleteLogic(PlanMessageSend model) {
        model.setDeleteStatus(true);
        return 1;
    }

    public List<PlanMessageSend> cancelPlanMessage(){
        Date now = new Date();
        List<PlanMessageSend> result = new ArrayList<>();
        QueryParam queryParam = new QueryParam();
        List<PlanMessageSend> planMessageSends = findList(queryParam);
        for(PlanMessageSend planMessageSend : planMessageSends){
            if (now.getTime() >= planMessageSend.getSendDate().getTime()) {
                result.add(planMessageSend);
            }
        }
        return result;
    }
}
